package com.sd.ws;
import com.alibaba.fastjson.JSONObject;
import org.java_websocket.client.WebSocketClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
@RestController
@RequestMapping("/websocket")
public class IndexController {

    /*@Autowired
    private ScoketClient webScoketClient;*/

    @Autowired
    private WebsocketClientFactory websocketClientFactory;

    @GetMapping("/login")
    public String login(String agentId){
        String entId = "1264";
        FlashAgentX flashAgentX = websocketClientFactory.createFlashAgentX(agentId, entId,agentId);
        return flashAgentX.toString();
    }

    @GetMapping("/message")
    public String sendMessage(String agentId,String message){
        String entId = "1264";
        FlashAgentX flashAgentX = websocketClientFactory.get(agentId);
        flashAgentX.doSend(message);
        return message;
    }

    @GetMapping("/messageJson")
    public String messageJson(String agentId,String message){
        String entId = "1264";
        FlashAgentX flashAgentX = websocketClientFactory.get(agentId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("message",message);
        String jsonString = jsonObject.toJSONString();
        String buf = flashAgentX.packHost("1000", jsonString);
        flashAgentX.doSend(buf);
        //flashAgentX.doSend(message);
        return message;
    }
    @GetMapping("/checkin")
    public void checkin(String agentId,String agentPwd,String extNum,String entPwd,String outsecond){
        String entId = "1264";
        String g_uri = "ws://47.104.94.106:8004/AcdGate/softPhone/";
        String g_TokenId = UUID.randomUUID().toString();
        String g_OptId = "100002";
        String g_WSUrl = g_uri + g_TokenId + "/" + g_OptId;
        System.out.println("g_WSUrl:"+g_WSUrl);
        FlashAgentX flashAgentX = websocketClientFactory.get(agentId);
        flashAgentX.checkin("89Sdfc88","100002","123456","60");
    }

    @GetMapping("/makecall")
    public void makecall(String agentId,String telno){
        FlashAgentX flashAgentX = websocketClientFactory.get(agentId);
        flashAgentX.makecall(telno,"");
    }

    @GetMapping("/checkout")
    public void checkout(String agentId,String agentPwd,String extNum,String entPwd,String outsecond){
        String entId = "1264";
        String g_uri = "ws://47.104.94.106:8004/AcdGate/softPhone/";
        String g_TokenId = UUID.randomUUID().toString();
        String g_OptId = "100002";
        String g_WSUrl = g_uri + g_TokenId + "/" + g_OptId;
        System.out.println("g_WSUrl:"+g_WSUrl);
        FlashAgentX flashAgentX = websocketClientFactory.get(agentId);
        flashAgentX.checkout();
    }
}
